package com.googlecode.bip.dao.mysql;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import com.googlecode.bip.dao.LocalDAO;
import com.googlecode.bip.dao.utils.DBHelper;
import com.googlecode.bip.entity.ILocal;
import com.googlecode.bip.entity.IllegalIdLocalException;
import com.googlecode.bip.entity.impl.Local;

/**
 *
 * @author Claude CHOISNET
 */
public class MySQLLocalDAO
      implements LocalDAO
{
    private static final long serialVersionUID = 2L;
    private final static transient Logger logger = Logger.getLogger( MySQLLocalDAO.class );
    private static transient Map<String,String> _localMap;

    /**
     * Create a new LocalDB
     */
    public MySQLLocalDAO()
    {
    }

    /**
     * Set LocalMap
     *
     * @param aLocalMap local Map to set
     */
    protected synchronized void setLocalMap( Map<String,String> aLocalMap )
    {
        MySQLLocalDAO._localMap = aLocalMap;
    }

    /**
     * Load LocalMap from database
     *
     * @throws NamingException if any
     * @throws SQLException if any
     */
    protected synchronized void loadLocalMap()
        throws NamingException, SQLException
    {
        Connection connection = MySQLDAOFactory.getConnection();

        try {
            setLocalMap(
                    DBHelper.getStringStringMap(
                        connection,
                        "SELECT * FROM `locals`",
                        "idlocal",
                        "name"
                        )
                    );
            }
        finally {
            MySQLDAOFactory.returnConnectionToPool( connection );
            }
    }

    @Override
    public ILocal findLocal( String idlocal ) throws NamingException,
            SQLException
    {
        return new Local( idlocal, getLocalMap().get( idlocal ) );
    }

    /**
     * Unsupported Operation
     *
     * @throw UnsupportedOperationException
     */
    public boolean insertLocal( final ILocal ilocal )
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Unsupported Operation
     *
     * @throw UnsupportedOperationException
     */
    public boolean updateLocal( final ILocal ilocal )
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Unsupported Operation
     *
     * @throw UnsupportedOperationException
     */
    public boolean deleteLocal( final ILocal ilocal )
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Returns an unmodifiable Map of (idlocal,name)
     * @return an unmodifiable Map of (idlocal,name)
     * @throws NamingException if any
     * @throws SQLException if any
     */
    @Override
    public Map<String,String> getLocalMap()
        throws NamingException, SQLException
    {
        if( _localMap == null ) {
            loadLocalMap();

            if( _localMap == null ) {
                logger.fatal( "Cant load 'local' table !!!!" );
                }
            }

        return Collections.unmodifiableMap( _localMap );
    }

    private final static Map<String,String> static_getLocalMap()
        throws NamingException, SQLException
    {
        if( _localMap == null ) {
            return new MySQLLocalDAO().getLocalMap();
            }

        return Collections.unmodifiableMap( _localMap );
    }

    /**
     * Use this when a new local entry is added
     */
    @Override
    public synchronized void reset()
    {
        _localMap = null;
    }

    public final static String static_getValidIdLocal( final String idlocal )
        throws IllegalIdLocalException
    {
        try {
            if( static_getLocalMap().keySet().contains( idlocal ) ) {
                return idlocal;
                }
            }
        catch( Exception e ) {
            throw new IllegalIdLocalException( idlocal, e );
            }

        throw new IllegalIdLocalException( idlocal );
    }

    @Override
    public String getValidIdLocal( final String idlocal )
        throws IllegalIdLocalException
    {
        return static_getValidIdLocal( idlocal );
    }
}
